var dataGrid;
$(function(){
	dataGrid = $('#dataGrid').datagrid({
		title:var_title,
		iconCls:'icon-tip',
		nowrap: false,
		striped: true,
		queryParams:{}, //查询条件  
		url: contextPath + '/posts/getPostsListData.action',
		sortName: 'createTime',
		sortOrder: 'desc',
		loadMsg:var_loadMsg, 
		remoteSort: false,
		idField:'id',
		fitColumns:true,
		/*singleSelect: true,*/
		pagination:true,
        rownumbers:true,
        scrollbarSize:0,
		columns:[[
            {field:'ck',checkbox:true},
            {title:var_column_serialnumber,field:'id',width:35,sortable:true,
            	sorter:function(a,b){
					return (a>b?1:-1);
				}
            },
			{title:var_column_postNum,field:'postNum',width:50,sortable:true},
			{title:var_column_postName,field:'postName',width:50,sortable:true},
			{title:var_column_deptId,field:'deptId',width:50,sortable:true},
			{title:var_column_description,field:'description',width:50,sortable:true},
			{field:'opt',title:var_column_opt,width:100,align:'center',
				formatter:function(value,rowData){
					var postsId = rowData.id;
					var optTemplate = "<a class=\"btn btn-mini ml5\" href=\"javascript:editPosts("+postsId+");\"><i class=\"icon-edit-data\"></i>"+var_column_edit+"</a>";
					   optTemplate += "<a class=\"btn btn-mini ml5\" href=\"javascript:delPosts("+postsId+");\"><i class=\"icon-remove-data\"></i>"+var_column_delete+"</a>";
					return optTemplate;
				}
			}
		]],
		onLoadSuccess:function(){ 
			//一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
			dataGrid.datagrid('clearSelections');
		}  
	});
	
	$('#searchPanel').panel({  
		width:"100%",  
		height:"100%", 
		/*collapsible:true,*/
		title: var_search_title
	});  
	
    // 当窗口大小发生变化时，调整DataGrid的大小  
    $(window).resize(function() { 
    	$('#searchPanel').panel('resize',{width:$(document.body).width() - 10});
    	dataGrid.resizeDataGrid(300);  
    });  
    
    //查询操作
    $("#queryPostsBtn").click(function(){
        var queryPostsForm = $("#queryPostsForm").serializeArray();
		var queryParams = dataGrid.datagrid('options').queryParams;
        $.each(queryPostsForm, function(i, field){
            queryParams[field.name] = field.value;
        });
        //重新加载datagrid的数据  
        dataGrid.datagrid('load');
    });
    
    //添加FOB成本模板
    $("#add-posts-btn").click(function(){
    	dataGrid.datagrid('clearSelections');
    	showDialog(var_add_title,contextPath+"/posts/toAddPosts.action");
    });
 
    $("#delete-posts-btn").click(function(){
    	var ids = [];
		var rows = dataGrid.datagrid('getSelections');
		if (rows.length == 0) {
        	$.messager.alert(var_alert_title, var_del_record_required, 'info');
        	return;
    	}
    	for (var i = 0; i < rows.length; i++) {
        	ids.push(rows[i].id);
    	}
    	delPosts(ids);
	});
    
});

function showDialog(title,url){
	parent.$.modalDialog({
		title : title,
		iconCls:"icon-tip",
		width:800,
		height:500,
		href : url,
		buttons:[{ 
			text : var_confirm,
			iconCls : 'icon-ok',
			handler : function() {
				parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		}, {
			text : var_cancel,
			iconCls : 'icon-cancel',
			handler : function() {
				parent.$.modalDialog.handler.dialog('close');
		   } 
		}]
	});
}

function editPosts(postsId){
	dataGrid.datagrid('clearSelections');
	showDialog(var_edit_title,contextPath+"/posts/toEditPosts.action?id="+postsId);
}

function delPosts(postsIds){
	$.messager.confirm(var_alert_title, var_del_confirm,function(r){
        if(r){
            $.ajax({
                url: contextPath+'/posts/delPosts.action',
                type: 'post',
                data: "postsIds="+postsIds,
                error: function(){
                	$.messager.alert(var_alert_title,var_opt_fail,'error');      
                },
                success: function(data){
                    if (data.result == 'success') {
                        $.messager.alert(var_alert_title, var_del_success,'info');
                        dataGrid.datagrid('reload');
                    }else{
                    	$.messager.alert(var_alert_title,var_opt_fail,'error');      
                    }          
                }
            });
        }
    });
}